package cn.JHai.rbac.service.imp;

import cn.JHai.rbac.comm.CurrentUtil;
import cn.JHai.rbac.comm.LoginException;
import cn.JHai.rbac.domain.Employee;
import cn.JHai.rbac.mapper.EmployeeMapper;
import cn.JHai.rbac.mapper.PermissionMapper;
import cn.JHai.rbac.query.PageResult;
import cn.JHai.rbac.query.QueryObject;
import cn.JHai.rbac.rsa.RsaKeys;
import cn.JHai.rbac.service.EmployeeService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class EmployeeImp implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RsaServiceImpl rsaService;

    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public int Delete(Long id) {
        employeeMapper.deleteRelation(id);
        return employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> findAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public int saveOrUpdata(Employee employee,Long ids[]) {
        //是插入
        if(employee.getId()==null){
            employeeMapper.insert(employee);
        }else {
            //跟新
            employeeMapper.deleteRelation(employee.getId());
            System.out.println(employee.getAdmin());
            employeeMapper.updateByPrimaryKey(employee);
        }

        if(ids!=null&&ids.length>0){
            for(int i = 0;i<ids.length;i++) {
                employeeMapper.insertEmployeeAndRoleRelation(employee.getId(),ids[i]);
            }
        }
        return 1;
    }

    @Override
    public PageResult query(QueryObject queryObject) {

        Long count = employeeMapper.selectForCount(queryObject);
        //List<Employee> employees =null;
        /*if(count<=3l){
            //employees = employeeMapper.selectAll();
        }
        else{
            //employees = employeeMapper.pageSelect(queryObject);
        }*/
        return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),employeeMapper.pageSelect(queryObject),count);
    }

    @SneakyThrows
    @Override
    public void login(String userName, String password) throws LoginException {

        //String s = rsaService.RSAEncryptDataPEM(password, RsaKeys.getServerPubKey());
        //System.out.println(s);
        Employee e = employeeMapper.login(userName);
        if(e!=null){
            String sp=rsaService.RSADecryptDataPEM(e.getPassword(), RsaKeys.getServerPrvKeyPkcs8());
            if(!sp.equals(password)){
                throw new LoginException();
            }

        }else {

            throw new LoginException();
        }
        CurrentUtil.setCurrent(e);

        Set<String> set = permissionMapper.selectExpressionByEmployId(e.getId());
        System.out.println(set);
        CurrentUtil.setExpression(set);


    }
}
